package com.ruoyi.operation.monitor.strategy;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.operation.domain.TFileResult;
import com.ruoyi.operation.domain.vo.FileMonitorVo;
import com.ruoyi.operation.monitor.IFileMonitorService;
import com.ruoyi.operation.monitor.IMonitorStrategy;
import com.ruoyi.system.api.RemoteTBsCalendarService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.warm.flow.core.utils.IdUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author Zzneko
 * @Date 2025/7/23 14:46
 * @Descprition
 **/
@Slf4j
@Service("2")
public class MonitorTwoStrategy implements IMonitorStrategy {

    @DubboReference
    private RemoteTBsCalendarService remoteTBsCalendarService;

    @Override
    public List<TFileResult> handleMonitorResult(List<FileMonitorVo> fileMonitorVos) {
        List<TFileResult> resultList = new ArrayList<>();
        //按照每个组进行分类
        Map<String, List<FileMonitorVo>> collect = fileMonitorVos.stream().collect(Collectors.groupingBy(FileMonitorVo::getGroupId));
        collect.forEach((groupId,values)->{
            TFileResult fileResult = checkOneByList(values);
            fileResult.setCreateBy("progress");
            fileResult.setCreateTime(new Date());
            fileResult.setTestdate(DateUtils.getNowDate());
            fileResult.setFilegroupid(groupId);
            fileResult.setId(IdUtils.nextIdStr());
            resultList.add(fileResult);
        });
        return resultList;
    }

    /**
     * 直接对比改路径下是否含有改文件。
     * @param values
     * @return
     */
    private TFileResult checkOneByList(List<FileMonitorVo> values) {
        TFileResult fileResult = new TFileResult();
        String memo = "";
        List<Boolean> collect = values.stream().map(this::replaceEntity).map(x -> {
            File file = new File(x.getFullpath()+"/"+x.getChilepath());
            if (!file.exists()) {
                log.error("配置目录不存在");
                fileResult.setResultmemo(memo+x.getFileName()+"文件不存在"+"\n");
                return Boolean.FALSE;
            }
            List<String> fileList = Arrays.stream(file.listFiles()).map(File::getName).collect(Collectors.toList());
            if (!fileList.contains(x.getFileName())) {
                fileResult.setResultmemo(memo+x.getFileName()+"比对结果不对"+"\n");
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        }).collect(Collectors.toList());
        if (collect.contains(Boolean.FALSE)) {
            fileResult.setResultvalue("0");
        }else {
            fileResult.setResultvalue("1");
        }
        return fileResult;
    }

    private FileMonitorVo replaceEntity(FileMonitorVo fileMonitorVo){
        //若有占位符直接替换当日日期
        String fullpath = fileMonitorVo.getFullpath();
        String childPath = fileMonitorVo.getChilepath();
        String fileName = fileMonitorVo.getFileName();
        if (fullpath.contains("{yyyyMMdd}")){
//            /rest/{yyyyMMdd}/  -> /rest/20250807/
            fullpath = fullpath.replace("{yyyyMMdd}", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            fileMonitorVo.setFullpath(fullpath);
        }
        if(StrUtil.isBlank(childPath)){
            fileMonitorVo.setChilepath("");
        }
        if(StrUtil.isNotBlank(childPath)&&childPath.contains("{yyyyMMdd}")){
            childPath = childPath.replace("{yyyyMMdd}", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            fileMonitorVo.setChilepath(childPath);
        }
        if(fileName.contains("{yyyyMMdd}")){
            fileName = fileName.replace("{yyyyMMdd}", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            fileMonitorVo.setFileName(fileName);
        }
        return fileMonitorVo;
    }

}
